package com.hyyt.shopjsp.basic.controller;

import com.hyyt.shopjsp.basic.pojo.BasicActor;
import com.hyyt.shopjsp.basic.pojo.BasicUserActor;
import com.hyyt.shopjsp.basic.service.IBasicActorService;
import com.hyyt.shopjsp.basic.service.IBasicUserActorService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Zhang Dingfang on 2016/10/26 0026下午 3:58.
 */
@Controller
@RequestMapping("/back/usersActor")
public class BasicUsersActorController extends BaseController {
    private Logger log = Logger.getLogger(BasicUsersActorController.class);
    private String basePath = "/back/basic/usersActor/";

    @Resource
    private IBasicUserActorService basicUserActorService;

    @Resource
    private IBasicActorService basicActorService;

    /**
     * 根据用户姓名和用户id获得角色集合
     *
     * @param usersId
     * @return
     */
    @RequestMapping(value = "gotoUsersActorPage")
    public String gotoUsersActorPage(Integer usersId,Map<String, Object> param) {
        param.put("usersId", usersId);
        return basePath + "usersActorList";
    }

    /**
     * 查询指定管理员已分配的所有角色
     *
     * @param usersId
     * @param page
     * @param response
     * @return
     */
    @RequestMapping(value = "listSelectedBasicActorByUsersId", method = RequestMethod.POST)
    @ResponseBody
    public void listSelectedBasicActorByUsersId(Integer usersId, String actorName, Page page, HttpServletResponse response) {
        Map<String, Object> param = new HashMap<>();
        param.put("usersId",usersId);
        if (Utils.objectIsNotEmpty(actorName)) {
            param.put("actorName", actorName);
        }
        int totalRecordCount = basicActorService.countSelectedBasicActorByUsersId(param);
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        param.put("sortColumns", "actorId desc");
        List<Map<String,Object>> list = basicActorService.querySelectedBasicActorByUsersId(param, page.getPageRecordBeginIndex(), page.getPageSize());
        param.clear();
        param.put("total", totalRecordCount);  //total键 存放总记录数
        param.put("rows", list);  //rows键 存放每页记录 list
        ResultMsg.create(param, response);  //把内容发送到页面
    }

    /**
     * 查询指定管理员未分配的所有角色
     *
     * @param usersId
     * @param page
     * @param response
     * @return
     */
    @RequestMapping(value = "listUnselectBasicActorByUsersId", method = RequestMethod.POST)
    @ResponseBody
    public void listUnselectBasicActorByUsersId(Integer usersId,String actorName, Page page, HttpServletResponse response) {
        Map<String, Object> param = new HashMap<>();
        param.put("usersId",usersId);
        if (Utils.objectIsNotEmpty(actorName)) {
            param.put("actorName", actorName);
        }
        int totalRecordCount = basicActorService.countUnselectBasicActorByUsersId(param);
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        param.put("sortColumns", "actorId desc");
        List<BasicActor> list = basicActorService.queryUnselectBasicActorByUsersId(param, page.getPageRecordBeginIndex(), page.getPageSize());
        param.clear();
        param.put("total", totalRecordCount);// total键 存放总记录数，必须的
        param.put("rows", list);// rows键 存放每页记录 list
        ResultMsg.create(param, response);  //把内容发送到页面
    }


    /**
     * 删除角色id集合
     *
     *@param usersId 管理员id
     * @param actorId 角色id
     * @return
     */
    @RequestMapping(value = "deleteUsersActor", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteUsersActor(Integer usersId,Integer actorId) {
        if (Utils.objectIsNotEmpty(usersId)&&Utils.objectIsNotEmpty(actorId)) {
            basicUserActorService.deleteBasicUserActor(usersId,actorId);
            return ResultMsg.create(true);
        }else{
            return ResultMsg.create(false, "删除失败，请重新操作");
        }
    }


    /**
     * 保存新的角色给用户
     *
     * @param usersId
     * @param actorIds  角色id 拼串字符
     * @param actorNames  角色名称 拼串字符
     * @return
     */
    @RequestMapping(value = "saveOrUpdateUsersActor", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdateUsersActor(Integer usersId, String actorIds,String actorNames) {
        if (Utils.objectIsNotEmpty(usersId) && Utils.objectIsNotEmpty(actorIds) && Utils.objectIsNotEmpty(actorNames)) {
            basicUserActorService.saveBasicUserActor(usersId,actorIds,actorNames);
            return ResultMsg.create(true);
        }else{
            return ResultMsg.create(false, "保存失败，请重新操作");
        }
    }
}